package com.example.frank.login.HttpPost;

import android.support.v7.app.AppCompatActivity;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Frank on 2018/4/25.
 */

public class HttpPost extends AppCompatActivity {

    public static String getReultForHttpPost(String val,String result) throws IOException {
        //服务器  ：服务器项目  ：servlet名称
        String path="http://103.45.105.41:3600/users/save_data";
        org.apache.http.client.methods.HttpPost httpPost=new org.apache.http.client.methods.HttpPost(path);
        List<NameValuePair> list=new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("up_data", val));
        httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));

        try {
            HttpResponse response=new DefaultHttpClient().execute(httpPost);
            if(response.getStatusLine().getStatusCode()==200){
                HttpEntity entity=response.getEntity();
                result= EntityUtils.toString(entity, HTTP.UTF_8);
            }else{
                result = "error";
            }
        }catch(Exception e){
            result = e.toString();

        }
        return result;
    }
}
